package chain_of_responsibility;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: linKe
 * @Date: 2022/1/14 22:56
 * @FileName: FilterChain
 * @version: 1.0
 * @Description: 过滤器链
 */
public class FilterChain implements Filter{

    private List<Filter> filters = new ArrayList<Filter>();
    /**
     * 位置计数器
     */
    private int index = 0;

    public void addFilter(Filter filter) {
        filters.add(filter);
    }

    @Override
    public void doFilter(Request request, Response response, FilterChain chain) {
        if(index >= filters.size()) {
            return;
        }
        Filter filter = filters.get(index ++);

        filter.doFilter(request, response, chain);
    }
}
